package com.dman.multi.proto;

import java.io.IOException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

import com.dman.multi.proxy.ProxyAdaptor;

public interface ProtocolHandler {
	/**
	 * The init() method is the most important method of any ProtocolHandler. This
	 * is because, this method is used to populate the size and the range supported
	 * variables in the <a href="../Section">Section</a>.
	 * @param url
	 * @param offset
	 * @return
	 * 1 * size of section, when range is supported
	 * -1 * size of file, when range is not supported
	 * null if unable to connect to the server
	 * @throws IOException
	 * @throws UnknownHostException
	 */
	public Long init(URL url, long offset, ProxyAdaptor proxy) throws IOException, UnknownHostException;
	
	/**
	 * This function gets the channel corresponding to the connected socket. This
	 * also sets the blocking mode to false.
	 * @param none
	 * @return
	 * The underlying socket channel.
	 * @throws IOException
	 */
	public SocketChannel socketChannel() throws IOException;
	
	/**
	 * This function reads the channel and returns a byte buffer according to the 
	 * transfer encoding specified. 
	 * @return
	 * A ByteBuffer
	 * @throws IOException
	 */
	public ByteBuffer read() throws IOException;
	
	/**
	 * This function closes the sockets and the channels associated with it to perform
	 * the clean-up after this section completes download.
	 * @param none
	 * @return none
	 * @throws IOException
	 */
	public void close() throws IOException;
}
